package fr.labri.harmony.analysis.jsep.results;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fr.labri.harmony.analysis.jsep.results.filter.Utils;

public class Counter {

	Map<Pair<String,String>,Integer> scores = new HashMap<>();
	List<String> dates = new ArrayList<String>();
	Map<Pair<String,String>,List<Pair<String,String>>> comments = new HashMap<Pair<String,String>,List<Pair<String,String>>>();

	public Counter() {
	}

	void resetAll() {
		dates = new ArrayList<String>();
		scores = new HashMap<>();
		comments = new HashMap<Pair<String,String>,List<Pair<String,String>>>();
	}

	public void incrDate(String a, String b, long date, boolean ts) {
		if(ts)
			dates.add(a+","+b+","+date);
		else
			dates.add(a+","+b+","+Utils.formatDate(date));
	}


	public void incrDateCat(int idxCat, long date, boolean ts) {
		if(ts)
			dates.add(idxCat+","+date);
		else
			dates.add(idxCat+","+Utils.formatDate(date));

	}

	public void viewCSVDateCat(OutputStream os) {
		PrintStream ps = new PrintStream(os);
		ps.println("category,date");
		for(String date : dates) {
			ps.println(date);
		}
		ps.close();
	}

	public void viewCSVDate(OutputStream os) {
		PrintStream ps = new PrintStream(os);
		ps.println("source,target,date");
		for(String date : dates) {
			ps.println(date);
		}
		ps.close();
	}

	public void viewCSVCommitLogs(OutputStream os) {
		PrintStream ps = new PrintStream(os);
		ps.println("source;target;project;reason");
		for(Pair<String,String> couple : comments.keySet()) {
			for(Pair<String,String> reason : comments.get(couple))
				ps.println("\""+couple.getElement0()+"\";\""+couple.getElement1()+"\";\""+reason.getElement0()+"\";\""+reason.getElement1().replaceAll("\"", "\'")+"\"");
		}
	}

	public void incrScore(String a, String b) {
		Pair<String,String> pair = new Pair<>(a, b);
		if(!scores.containsKey(pair)) {
			scores.put(pair, 0);
		}
		scores.put(pair, scores.get(pair)+1);
	}

	public void incrComment(String source, String target, String project, String message) {
		Pair<String,String> pair = new Pair<>(source, target);
		if(!comments.containsKey(pair)) {
			comments.put(pair, new ArrayList<Pair<String,String>>());
		}
		String log = Utils.optimizeString(message, source, target);
		if(!log.isEmpty())
			comments.get(pair).add(new Pair<String, String>(project, log));
	}

	public void incrCommentBugTracker(String source, String target, String project, Pair<String,String> message) {
		Pair<String,String> pair = new Pair<>(source, target);
		if(!comments.containsKey(pair)) {
			comments.put(pair, new ArrayList<Pair<String,String>>());
		}
		String log = Utils.optimizeString(message.getElement1(), source, target);
		if(!log.isEmpty()) {
			comments.get(pair).add(new Pair<String, String>(project, "("+message.getElement0()+")\n"+log));
		}
	}

	public Map<Pair<String,String>,Integer> getScores() {
		return scores;
	}

	public void sortedAffichage() {
		Set<Integer> set = new HashSet<Integer>(scores.values());
		List<Integer> values = new ArrayList<Integer>(set);
		Collections.sort(values);
		Collections.reverse(values);
		Set<String> libs = new HashSet<String>();
		//		for(Pair<String,String> pair : scores.keySet()) {
		//			libs.add(pair.getElement0());
		//			libs.add(pair.getElement1());
		//		}
		//		for(String lib : libs) {
		//			System.out.println("graph.addNodes('"+lib+"');");
		//		}
		for(int v : values) {
			for(Pair<String,String> pair : scores.keySet()) {
				if(scores.get(pair)==v)
					System.out.println(""+pair.getElement0()+","+pair.getElement1() + ","+v);
			}
		}
	}


}
